package com.yaodian100.ec.order.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.order.entity.ServiceOrderDetail;
import com.yaodian100.ec.order.entity.ServiceOrders;
import com.yaodian100.ec.order.service.ServiceOrderService;

/**
 * @author jinwei.lin
 * 
 */
public class ServiceOrderAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = 178887580319281383L;
	/** logger */
	private final Logger logger = LoggerFactory.getLogger(ServiceOrderAction.class);

	private Menu orderStMenu; // 與 getter, setter
	private Menu paymentStMenu; // 與 getter, setter
	private Menu paymentTpMenu; // 與 getter, setter
	private Menu logisticStMenu; // 與 getter, setter
	private Menu returnStMenu; // 與 getter, setter

	private String receiverName;
	private String receiverMobile;
	private String receiverPhoneArea;
	private String receiverPhone;
	private String receiverExt;
	private String receiverZip;
	private String receiverAddr;
	private String receiverProvince;
	private String receiverCity;
	private String receiverArea;
	private String invoiceTitle;

	private DomainService<Menu> menuService; // 與 setter
	private ServiceOrderService serviceOrderService;
	private List<ServiceOrders> serviceOrdersList;
	private List<ServiceOrderDetail> serviceOrderDetailList;
	private String serviceId;
	private ServiceOrders serviceOrder;
	private SimplePager pager;

	// 查詢條件
	private String orderIdParam;
	private String serviceIdParam;
	private String serviceDtStParam;
	private String serviceDtEdParam;
	private String loginIdParam;
	private String orderStParam;
	private int serviceTpParam;
	private String minDate;
	private String today;

	/**
	 * 檢視订单資料
	 * 
	 * @return
	 */
	public String view() throws Exception {
		if (StringUtils.isNotBlank(serviceId) || serviceOrder.getServiceId() != null) {
			try {
				if (null != serviceId)
					serviceOrder = serviceOrderService.get(serviceId);
				else
					serviceOrder = serviceOrderService.get(serviceOrder.getServiceId());
				CommonCriteria crit = new CommonCriteria();
				crit.addEq("serviceId", serviceOrder.getServiceId());
				serviceOrderDetailList = serviceOrderService.getServiceOrderDetailList(0, -1, crit, null);
			} catch (CoreException e) {
				addActionError(e.getMessage());
				e.printStackTrace();
			}
		}
		return "view";
	}

	/**
	 * 订单資料列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String query() throws Exception {
		CommonCriteria crit = new CommonCriteria();

		if (StringUtils.isBlank(serviceDtStParam))
			serviceDtStParam = DateUtil.convertDateToString("yyyy/MM/dd", DateUtil.getToday().getTime());
		if (StringUtils.isBlank(serviceDtEdParam))
			serviceDtEdParam = DateUtil.convertDateToString("yyyy/MM/dd", DateUtil.getToday().getTime());

		DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

		crit.addGe("serviceDt", format.parse(serviceDtStParam + " 00:00:00"));
		crit.addLe("serviceDt", format.parse(serviceDtEdParam + " 23:59:59"));

		if (StringUtils.isNotBlank(serviceIdParam))
			crit.addRlike("serviceId", serviceIdParam);
		if (StringUtils.isNotBlank(orderIdParam))
			crit.addEq("orderId", orderIdParam);
		if (StringUtils.isNotBlank(loginIdParam))
			crit.addRlike("loginId", loginIdParam);
		if (StringUtils.isNotBlank(orderStParam))
			crit.addEq("orderSt", orderStParam);
		crit.addEq("serviceTp", serviceTpParam);

		if (pager == null)
			pager = new SimplePager();
		// pager.setPageRecord(1);
		int entitySize = serviceOrderService.getListSize(crit).intValue();
		pager.setTotalSize(entitySize);

		serviceOrdersList = serviceOrderService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager
				.getPageRecord(), crit, null);
		// logger.info("pager:{},orders:{}", pager, orders);

		return "list";
	}

	/**
	 * 查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String search() throws Exception {
		return "search";
	}

	public void prepare() throws Exception {
		clearErrorsAndMessages();
		if (org.apache.commons.lang.StringUtils.isBlank(serviceDtStParam))
			serviceDtStParam = DateUtil.convertDateToString("yyyy/MM/dd", DateUtil.getToday().getTime());
		if (org.apache.commons.lang.StringUtils.isBlank(serviceDtEdParam))
			serviceDtEdParam = DateUtil.convertDateToString("yyyy/MM/dd", DateUtil.getToday().getTime());
		if (StringUtils.isBlank(minDate))
			minDate = DateUtil.convertDateToString("yyyy,MM - 1,dd", DateUtil.getTodayAddDays(-50).getTime());
		if (StringUtils.isBlank(today))
			today = DateUtil.convertDateToString("yyyy,MM - 1,dd", DateUtil.getToday().getTime());
		serviceTpParam = 1;
		if (orderStMenu == null) {
			orderStMenu = menuService.get("Order.status");
		}
		if (logisticStMenu == null) {
			logisticStMenu = menuService.get("Order.Prod.LogisticSt");
		}
		if (paymentStMenu == null) {
			paymentStMenu = menuService.get("Order.paymentSt");
		}
		if (paymentTpMenu == null) {
			paymentTpMenu = menuService.get("Order.PaymentTp");
		}
		if (returnStMenu == null) {
			returnStMenu = menuService.get("Order.Prod.ReturnSt");
		}
	}

	public String getToday() {
		return today;
	}

	public void setToday(String today) {
		this.today = today;
	}

	public Menu getOrderStMenu() {
		return orderStMenu;
	}

	public void setOrderStMenu(Menu orderStMenu) {
		this.orderStMenu = orderStMenu;
	}

	public Menu getPaymentStMenu() {
		return paymentStMenu;
	}

	public void setPaymentStMenu(Menu paymentStMenu) {
		this.paymentStMenu = paymentStMenu;
	}

	public Menu getPaymentTpMenu() {
		return paymentTpMenu;
	}

	public void setPaymentTpMenu(Menu paymentTpMenu) {
		this.paymentTpMenu = paymentTpMenu;
	}

	public String getLoginIdParam() {
		return loginIdParam;
	}

	public void setLoginIdParam(String loginIdParam) {
		this.loginIdParam = loginIdParam;
	}

	public String getOrderStParam() {
		return orderStParam;
	}

	public void setOrderStParam(String orderStParam) {
		this.orderStParam = orderStParam;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public Menu getReturnStMenu() {
		return returnStMenu;
	}

	public void setReturnStMenu(Menu returnStMenu) {
		this.returnStMenu = returnStMenu;
	}

	public String getReceiverName() {
		return receiverName;
	}

	public void setReceiverName(String receiverName) {
		this.receiverName = receiverName;
	}

	public String getReceiverMobile() {
		return receiverMobile;
	}

	public void setReceiverMobile(String receiverMobile) {
		this.receiverMobile = receiverMobile;
	}

	public String getReceiverPhoneArea() {
		return receiverPhoneArea;
	}

	public void setReceiverPhoneArea(String receiverPhoneArea) {
		this.receiverPhoneArea = receiverPhoneArea;
	}

	public String getReceiverPhone() {
		return receiverPhone;
	}

	public void setReceiverPhone(String receiverPhone) {
		this.receiverPhone = receiverPhone;
	}

	public String getReceiverExt() {
		return receiverExt;
	}

	public void setReceiverExt(String receiverExt) {
		this.receiverExt = receiverExt;
	}

	public String getReceiverZip() {
		return receiverZip;
	}

	public void setReceiverZip(String receiverZip) {
		this.receiverZip = receiverZip;
	}

	public String getReceiverAddr() {
		return receiverAddr;
	}

	public void setReceiverAddr(String receiverAddr) {
		this.receiverAddr = receiverAddr;
	}

	public String getReceiverProvince() {
		return receiverProvince;
	}

	public void setReceiverProvince(String receiverProvince) {
		this.receiverProvince = receiverProvince;
	}

	public String getReceiverCity() {
		return receiverCity;
	}

	public void setReceiverCity(String receiverCity) {
		this.receiverCity = receiverCity;
	}

	public String getReceiverArea() {
		return receiverArea;
	}

	public void setReceiverArea(String receiverArea) {
		this.receiverArea = receiverArea;
	}

	public String getInvoiceTitle() {
		return invoiceTitle;
	}

	public void setInvoiceTitle(String invoiceTitle) {
		this.invoiceTitle = invoiceTitle;
	}

	public Menu getLogisticStMenu() {
		return logisticStMenu;
	}

	public void setLogisticStMenu(Menu logisticStMenu) {
		this.logisticStMenu = logisticStMenu;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public String getMinDate() {
		return minDate;
	}

	public void setMinDate(String minDate) {
		this.minDate = minDate;
	}

	public List<ServiceOrders> getServiceOrdersList() {
		return serviceOrdersList;
	}

	public void setServiceOrdersList(List<ServiceOrders> serviceOrdersList) {
		this.serviceOrdersList = serviceOrdersList;
	}

	public List<ServiceOrderDetail> getServiceOrderDetailList() {
		return serviceOrderDetailList;
	}

	public void setServiceOrderDetailList(List<ServiceOrderDetail> serviceOrderDetailList) {
		this.serviceOrderDetailList = serviceOrderDetailList;
	}

	public String getServiceId() {
		return serviceId;
	}

	public void setServiceId(String serviceId) {
		this.serviceId = serviceId;
	}

	public ServiceOrders getServiceOrder() {
		return serviceOrder;
	}

	public void setServiceOrder(ServiceOrders serviceOrder) {
		this.serviceOrder = serviceOrder;
	}

	public String getOrderIdParam() {
		return orderIdParam;
	}

	public void setOrderIdParam(String orderIdParam) {
		this.orderIdParam = orderIdParam;
	}

	public String getServiceIdParam() {
		return serviceIdParam;
	}

	public void setServiceIdParam(String serviceIdParam) {
		this.serviceIdParam = serviceIdParam;
	}

	public int getServiceTpParam() {
		return serviceTpParam;
	}

	public void setServiceTpParam(int serviceTpParam) {
		this.serviceTpParam = serviceTpParam;
	}

	public void setServiceOrderService(ServiceOrderService serviceOrderService) {
		this.serviceOrderService = serviceOrderService;
	}

	public String getServiceDtStParam() {
		return serviceDtStParam;
	}

	public void setServiceDtStParam(String serviceDtStParam) {
		this.serviceDtStParam = serviceDtStParam;
	}

	public String getServiceDtEdParam() {
		return serviceDtEdParam;
	}

	public void setServiceDtEdParam(String serviceDtEdParam) {
		this.serviceDtEdParam = serviceDtEdParam;
	}

}